/**
 * Copyright (c) 2010-2020 by skzr.org
 * All rights reserved.
 * @Author <a href="mailto:skzr.org@gmail.com">skzr.org</a>
 * @date 2011-3-14 下午11:53:55
 */
package org.skzr.logging.impl;

import java.io.Serializable;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 消息解析器
 * @author <a href="mailto:skzr.org@gmail.com">skzr.org</a>
 * @version 1.0.0
 * @since JDK1.6
 */
public class MessageConveyor implements Serializable {
	private static final long serialVersionUID = 1L;
	private final Locale locale; // NOPMD by skzrorg on 11-3-22 上午12:54
	/** Map&lt;对应类名, 对应的MessageProperties&gt;. */
	private final transient Map<String, MessageProperties> cache = new ConcurrentHashMap<String, MessageProperties>();
	
	public MessageConveyor(Locale locale) {
		this.locale = locale;
	}
	
	/**
	 * 获取国际化数据
	 * @param <E>
	 * @param key
	 * @return 国际化数据器
	 */
	private <E extends Enum<?>> MessageProperties getMessageProperties(E key) {
		final String clazz = key.getClass().getName();
		MessageProperties messageProperties = cache.get(clazz);
		
		if (messageProperties == null) {
			messageProperties = MessageProperties.initMessageProperties(key.getDeclaringClass());
			cache.put(clazz, messageProperties);
		}
		return messageProperties;
	}
	
	/**
	 * 获取国际化文本
	 * @param <E> 国际化必须是{@link org.skzr.logging.stereotype.BaseName BaseName}注解的枚举
	 * @param key 国际化Key
	 * @return key对应的国际化文本
	 */
	public <E extends Enum<?>> String getMessage(E key) {
		return key == null ? null
				: getMessageProperties(key).findMessage(key, locale);
	}

}
